package org.x.user.controller;

import com.mybatisflex.core.paginate.Page;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.x.common.config.Result;
import org.x.common.enums.LogAsync;
import org.x.user.dto.*;
import org.x.user.service.ISysDeptService;

import java.util.List;

/**
 * 部门表 控制层。
 *
 * @author mybatis-flex-helper automatic generation
 * @since 1.0
 */
@RestController
@RequestMapping("/api/dept")
public class SysDeptController {

    @Autowired
    private ISysDeptService sysDeptService;

    @LogAsync("创建部门")
    @PostMapping("/create")
    public Result<Long> create(@RequestBody @Valid CreateDeptDTO createDeptDTO) {
        return Result.ok(sysDeptService.create(createDeptDTO));
    }

    @LogAsync("修改部门")
    @PutMapping("/update")
    public Result update(@RequestBody @Valid UpdateDeptDTO updateDeptDTO) {
        sysDeptService.update(updateDeptDTO);
        return Result.ok();
    }

    @LogAsync("删除部门")
    @DeleteMapping("/delete/{deptId}")
    public Result delete(@PathVariable Long deptId) {
        sysDeptService.deleteById(deptId);
        return Result.ok();
    }

    @LogAsync("查询部门")
    @GetMapping("/list")
    public Result list(@ModelAttribute DeptQueryDTO deptQueryDTO) {
        return Result.ok(sysDeptService.list(deptQueryDTO));
    }

    @LogAsync("查询部门详情")
    @GetMapping("/detail/{deptId}")
    public Result detail(@PathVariable Long deptId) {
        return Result.ok(sysDeptService.detail(deptId));
    }

    @LogAsync("部门树查询")
    @GetMapping("/tree")
    public Result tree() {
        return Result.ok(sysDeptService.tree());
    }

    @LogAsync("查询部门用户列表")
    @GetMapping("/users")
    public Result userList(@ModelAttribute @Valid DeptUserDTO deptUserDTO) {
        return Result.ok(sysDeptService.userList(deptUserDTO));
    }

    @LogAsync("部门结构调整")
    @PutMapping("/structure")
    public Result structure(@RequestBody @Valid List<DeptStructureDTO> changes) {
        sysDeptService.structure(changes);
        return Result.ok();
    }
}